package com.gillion.grule.client.core.variable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import java.util.Map;
import java.util.Objects;
import java.util.Set;





public class AssemblyVariable
        extends BaseVariable
{
    @JsonIgnore
    private PackageVariable packageVariable;
    private Map<String, RuleVariable> ruleVariables = Maps.newConcurrentMap();


    public Object getVariable(String name) {
        Object t = this.variables.get(name);
        return (t != null) ? t : this.packageVariable.getVariable(name);
    }



    public void init() {
        super.init();
        Set<String> keys = this.ruleVariables.keySet();
        for (String key : keys) {
            ((RuleVariable)this.ruleVariables.get(key)).init();
        }
    }


    public String toString() {
        return "AssemblyVariable{}";
    }


    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof AssemblyVariable)) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }

        AssemblyVariable that = (AssemblyVariable)o;

        if ((this.ruleVariables != null) ? !this.ruleVariables.equals(that.ruleVariables) : (that.ruleVariables != null)) return false;


        return
                Objects.equals(ruleVariables, that.ruleVariables) &&
                Objects.equals(packageVariable, that.packageVariable);

    }


    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + ((this.ruleVariables != null) ? this.ruleVariables.hashCode() : 0);
        return result;
    }

    public PackageVariable getPackageVariable() {
        return this.packageVariable;
    }

    public void setPackageVariable(PackageVariable packageVariable) {
        this.packageVariable = packageVariable;
    }

    public Map<String, RuleVariable> getRuleVariables() {
        return this.ruleVariables;
    }

    public void setRuleVariables(Map<String, RuleVariable> ruleVariables) {
        this.ruleVariables = ruleVariables;
    }
}